<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$subject_id = $_GET['id'];

$get_subject_data = mysql_query("SELECT * FROM course_subjects WHERE id = $subject_id");
$subject_name = mysql_result($get_subject_data,0,'subject_title');
$abbreviation = mysql_result($get_subject_data,0,'abbreviation');

?>
<p><a href="index.php">&lt;&lt; Back to Courses Portal</a></p>
<?php
echo "<h1>$subject_name Courses</h1>";
echo '<p>';
//creates link/button for add Course
echo "<a href='add.php?sid=$subject_id'>Add Course</a>";
echo '</p>';

//counting variable
$i = 0;

//select statement
$query="SELECT * FROM courses WHERE course_subject_id = $subject_id ORDER BY course_number";
$result=mysql_query($query);

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

$find_insert=mysql_query("SELECT * FROM courses");
$insert_point=mysql_numrows($find_insert);

//add timeslot will be done at one past last timeslot currently in table
$insert_point++;

if ($num_rows > 0)
{
	echo '<p>';

	//start table
	echo "<table class=\"postSchedule\">";
		
		//create table row with column ID's
		echo "<tr>";
			echo "<td><b>Course Number</b></td>";
			echo "<td><b>Course Name</b></td>";
			echo "<td style='text-align: center'><b>Credits</b></td>";
			echo "<td style='text-align: center'><b>Graduate</b></td>";
			echo "<td style='text-align: center'><b>Lab</b></td>";
			echo "<td></td>";
			echo "<td></td>";
			echo "<td></td>";
		echo "</tr>";

	//loop to walk through and print to screen all entries in course_subjects table
	$row_count=0;
	while ($i < $num_rows) 
	{
		$row_count++;
		//store each subject name and abbreviation in a scalar
		$course_id=mysql_result($result,$i,"id");
		$course_number=mysql_result($result,$i,"course_number");
		$course_title=mysql_result($result,$i,"title");
		$hours_per_week=mysql_result($result,$i,"hours_per_week");
		$lab_id=mysql_result($result,$i,"lab_number");
		$graduate_level=mysql_result($result,$i,"graduate_level");
		
		//generate table row for each subject on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td>$abbreviation $course_number</td>";		//course number cell
			echo "<td>$course_title</td>";
			echo "<td style='text-align: center'>$hours_per_week</td>";		
			
			if ($graduate_level)
			{
				echo "<td style='text-align: center'>Yes</td>";
			}
			else
			{
				echo "<td style='text-align: center'>No</td>";
			}
			
			if ($lab_id)
			{
				$get_lab_number=mysql_query("SELECT * FROM courses WHERE id=$lab_id");
				$lab_number=mysql_result($get_lab_number,0,"course_number");
				echo "<td style='text-align: center'>$abbreviation $lab_number</td>";
			}
			else
			{
				echo "<td style='text-align: center'>-</td>";
			}
			
			echo "<td>";		//cell containing form for edit link/button
				echo "<a href='edit.php?id=$course_id&amp;sid=$subject_id'>Edit</a>";
			echo "</td>";
			
			echo "<td>";		//cell containing form for delete link/button
				echo "<a href='delete.php?id=$course_id&amp;sid=$subject_id'>Delete</a>";
			echo "</td>";
			
			echo "<td>";		//cell containing form for update sections link/button
				echo "<a href='sections/?id=$course_id&amp;sid=$subject_id'>View Sections</a>";
			echo "</td>";
		echo "</tr>";
			
		//increment counting variable
		$i++;
	}

	//end table tag
	echo "</table>";

	echo '</p>';
}
else
{
	echo "<p><b>NO COURSES CURRENTLY ASSIGNED TO SUBJECT</b></p>";
}


?>


<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>